//
//  BLStatusCell.m
//  新浪微博
//
//  Created by hbl on 16/2/22.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLStatusCell.h"
#import "BLStatusFrame.h"
#import "BLStatus.h"
#import "BLUser.h"
#import "UIImageView+WebCache.h"
#import "BLStatusToolBar.h"
#import "BLPhotosView.h"
#import "BLPhotosView.h"

#define BLColor(r,g,b) [UIColor colorWithRed:r/255 green:g/255 blue:b/255 alpha:1]
#define nameFont [UIFont systemFontOfSize:15]
#define timeFont [UIFont systemFontOfSize:12]
#define sourceFont [UIFont systemFontOfSize:12]
#define contentFont [UIFont systemFontOfSize:15]
#define retweetnameFont [UIFont systemFontOfSize:14]
#define retweetcontentTextFont [UIFont systemFontOfSize:14]
#define toolBarFont [UIFont systemFontOfSize:15]
@interface BLStatusCell()
//1部分
@property (nonatomic, weak)UIImageView *topView;
@property (nonatomic, weak)UIImageView *iconView;
@property (nonatomic, weak)UIImageView *vipView;
@property (nonatomic, weak)BLPhotosView *photoView;

@property (nonatomic, weak)UILabel *nameLabel;
@property (nonatomic, weak)UILabel *timeLabel;
@property (nonatomic, weak)UILabel *sourceLabel;
@property (nonatomic, weak)UILabel *contentTextLabel;


//2部分
@property (nonatomic, weak)UIImageView *retweetView;
@property (nonatomic, weak)BLPhotosView *retweetphotoView;
@property (nonatomic, weak)UILabel *retweetnameLabel;
@property (nonatomic, weak)UILabel *retweetcontentTextLabel;


//3部分
@property (nonatomic, weak)BLStatusToolBar *toolBarView;

@end


@implementation BLStatusCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //设置原始微博子控件
        [self setupOriginalSubViews];
        //设置转发微博子控件
        [self setupRetweetSubViews];
        //设置底部工具栏子控件
        [self setupToolBarSubView];
        
    }
    return self;
}

- (void)setupOriginalSubViews
{
    //上部分主View
    UIImageView *topView = [[UIImageView alloc]init];
    [self.contentView addSubview:topView];
    self.topView = topView;
    
    //头像
    UIImageView *iconView = [[UIImageView alloc]init];
    [self.topView addSubview:iconView];
    self.iconView = iconView;
    
    //会员标志
    UIImageView *vipView = [[UIImageView alloc]init];
    [self.topView addSubview:vipView];
    self.vipView = vipView;
    
    //自己的配图
    BLPhotosView *photoView = [[BLPhotosView alloc]init];
    [self.topView addSubview:photoView];
    self.photoView = photoView;
    
    //用户昵称
    UILabel *nameLabel = [[UILabel alloc]init];
    [self.topView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    //发布时间
    UILabel *timeLabel = [[UILabel alloc]init];
    [self.topView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    //消息来源
    UILabel *sourceLabel = [[UILabel alloc]init];
    [self.topView addSubview:sourceLabel];
    self.sourceLabel = sourceLabel;
    
    //消息类容
    UILabel *contentTextLabel = [[UILabel alloc]init];
    [self.topView addSubview:contentTextLabel];
    self.contentTextLabel = contentTextLabel;


}

- (void)setupRetweetSubViews
{
    //转发微博主view
    UIImageView *retweetView = [[UIImageView alloc]init];
    [self.topView addSubview:retweetView];
    self.retweetView = retweetView;
    
    //转发微博配图
    BLPhotosView *retweetphotoView = [[BLPhotosView alloc]init];
    [self.retweetView addSubview:retweetphotoView];
    self.retweetphotoView = retweetphotoView;
    
    //转发微博人昵称
    UILabel *retweetnameLabel = [[UILabel alloc]init];
    [self.retweetView addSubview:retweetnameLabel];
    self.retweetnameLabel = retweetnameLabel;
    
    //转发微博内容
    UILabel *retweetcontentTextLabel = [[UILabel alloc]init];
    [self.retweetView addSubview:retweetcontentTextLabel];
    self.retweetcontentTextLabel = retweetcontentTextLabel;
    
}

- (void)setupToolBarSubView
{
    BLStatusToolBar *toolBarView = [[BLStatusToolBar alloc]init];
    [self.contentView addSubview:toolBarView];
    self.toolBarView = toolBarView;
}

- (void)setStatusFrame:(BLStatusFrame *)statusFrame
{
    _statusFrame = statusFrame;
    [self setupOriginalData];
    [self setupRetweetData];
    [self setupToolBarData];
}

- (void)setupOriginalData
{
    self.topView.frame = self.statusFrame.topViewF;
    UIImage *topViewImg = [UIImage imageNamed:@"common_card_background_os7"];
    topViewImg = [topViewImg resizableImageWithCapInsets:UIEdgeInsetsMake(topViewImg.size.height*0.5, topViewImg.size.width*0.5, topViewImg.size.height*0.5, topViewImg.size.width*0.5)];
    self.topView.image =topViewImg;
    self.topView.userInteractionEnabled = YES;
    
    self.iconView.frame = self.statusFrame.iconViewF;
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:self.statusFrame.status.user.profile_image_url]];
    self.iconView.clipsToBounds = YES;
    self.iconView.layer.cornerRadius = 17.5;
    
    if (self.statusFrame.status.user.verified_type > -1) {
        self.vipView.hidden = NO;
        self.vipView.frame = self.statusFrame.vipViewF;
        self.vipView.contentMode = UIViewContentModeCenter;
        self.vipView.image = [UIImage imageNamed:@"common_icon_membership_level6"];
    }else{
        self.vipView.hidden = YES;
    }
    
    
    if (self.statusFrame.status.thumbnail_pic) {
        self.photoView.hidden = NO;
        self.photoView.frame = self.statusFrame.photoViewF;
        
    }else{
        self.photoView.hidden = YES;
    }
    
    self.nameLabel.frame = self.statusFrame.nameLabelF;
    self.nameLabel.text = self.statusFrame.status.user.name;
    self.nameLabel.font = nameFont;
    
    self.timeLabel.frame = self.statusFrame.timeLabelF;
    self.timeLabel.text = self.statusFrame.status.created_at;
    self.timeLabel.font = timeFont;
    self.timeLabel.textColor = BLColor(244.0, 140.0, 19.0);
    
    self.sourceLabel.frame = self.statusFrame.sourceLabelF;
    self.sourceLabel.text = self.statusFrame.status.source;
    self.sourceLabel.font = sourceFont;
    self.sourceLabel.textColor = BLColor(92.0, 92.0, 92.0);
    
    self.contentTextLabel.frame = self.statusFrame.contentTextLabelF;
    self.contentTextLabel.text = self.statusFrame.status.text;
    self.contentTextLabel.numberOfLines = 0;
    self.contentTextLabel.font = contentFont;
    
    self.photoView.frame = self.statusFrame.photoViewF;
    self.photoView.photos = self.statusFrame.status.pic_urls;
    
    
    //转发部分--------------------------------------
    
    
}

- (void)setupRetweetData
{
    BLStatus *retweetedStatus = self.statusFrame.status.retweeted_status;
    
    self.retweetView.frame = self.statusFrame.retweetViewF;
    //timeline_card_middlebottom_highlighted
    UIImage *retweetViewImg = [UIImage imageNamed:@"timeline_retweet_background_os7"];
    retweetViewImg = [retweetViewImg resizableImageWithCapInsets:UIEdgeInsetsMake(retweetViewImg.size.height*0.5, retweetViewImg.size.width - 5, retweetViewImg.size.height*0.5, retweetViewImg.size.width - 5)];
    self.retweetView.userInteractionEnabled = YES;
    self.retweetView.image = retweetViewImg;

    self.retweetnameLabel.frame = self.statusFrame.retweetnameLabelF;
    self.retweetnameLabel.text = [NSString stringWithFormat:@"@%@",retweetedStatus.user.name];
    self.retweetnameLabel.textColor = BLColor(49.0, 72.0, 150.0);
    self.retweetnameLabel.font = retweetnameFont;
    
    if (retweetedStatus.pic_urls.count) {
        self.retweetphotoView.hidden = NO;
        self.retweetphotoView.frame = self.statusFrame.retweetphotoViewF;
        self.retweetphotoView.photos = retweetedStatus.pic_urls;
    }else{
        self.retweetphotoView.hidden = YES;
    }
    
    self.retweetcontentTextLabel.frame = self.statusFrame.retweetcontentTextLabelF;
    self.retweetcontentTextLabel.text = retweetedStatus.text;
    self.retweetcontentTextLabel.font = retweetcontentTextFont;
    self.retweetcontentTextLabel.textColor = BLColor(70.0, 70.0, 70.0);
    self.retweetcontentTextLabel.numberOfLines = 0;
}

- (void)setupToolBarData
{
    self.toolBarView.frame = self.statusFrame.toolBarViewF;
    self.toolBarView.status = self.statusFrame.status;
}
@end
